package com.autumn.aitraffic.service.impl;


import com.autumn.aitraffic.api.Result;
import com.autumn.aitraffic.component.JwtUtil;
import com.autumn.aitraffic.domain.po.Images;
import com.autumn.aitraffic.domain.po.User;
import com.autumn.aitraffic.domain.vo.*;
import com.autumn.aitraffic.mapper.UserMapper;

import com.autumn.aitraffic.service.IImagesService;
import com.autumn.aitraffic.service.IUserService;
import com.autumn.aitraffic.service.UserService;
import com.autumn.aitraffic.util.AliOSSUtils;
import com.autumn.aitraffic.util.FileUtil;
import com.autumn.aitraffic.util.HashMd5;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wzx
 * @since 2025-01-17
 */

@Component
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    HashMd5 hashMd5;
    @Autowired
    IImagesService imagesService;
    @Override
    public Result login(String username, String password) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        List<User> users = userMapper.selectByMap(map);
        if (users==null|| users.isEmpty()){
            return Result.fail("用户不存在",null);
        }
        User user = users.get(0);
        if(user.getPassword().equals(password)){
            return Result.succ("",new LoginVo(user.getLevel(),jwtUtil.sign(username,hashMd5.encodePassword(password))));
        }
        return Result.fail("用户名或密码有误",null);
    }

    @Override
    public Result regist(String username, String password, String telephone) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        List<User> users = userMapper.selectByMap(map);
        if(users.size()>0){
            return Result.fail("用户名已存在",null);
        }
        User user = new User().setUsername(username).setPassword(password).setTelephone(telephone);
        user.setLevel("1");
        userMapper.insert(user);
        return Result.succ(null,null);
    }

    @Override
    public Result faceReg(FaceAnalyzeVo faceAnalyzeVo) {
        //校验信息
        String username = faceAnalyzeVo.getUsername();
        User user = this.lambdaQuery().eq(User::getUsername, username).one();
        if (user==null){
            return Result.fail("用户名不存在",null);
        }
        Images images = new Images();
        images.setUserId(Long.valueOf(user.getId()));
        images.setImg(faceAnalyzeVo.getImage());
        imagesService.save(images);
        return Result.succ(null,null);
    }

    @Override
    public Result faceAnalyze(FaceAnalyzeVo faceAnalyzeVo) {
        User user = this.lambdaQuery().eq(User::getUsername, faceAnalyzeVo.getUsername()).one();
        if (user==null){
            return Result.fail("用户不存在",null);
        }
        Images images = imagesService.lambdaQuery().eq(Images::getUserId, user.getId()).one();
        if (images==null){
            return Result.fail("用户人脸信息不存在",null);
        }
        ImageVo imageVo=new ImageVo();
        imageVo.setBase64Image(images.getImg());
        return Result.succ(null,imageVo);
    }

    @Override
    public Result getUserList(Page page, User user) {

        Page<User> myPage = lambdaQuery().like(user.getUsername() != null, User::getUsername, user.getUsername())
                .like(user.getTelephone() != null, User::getTelephone, user.getTelephone())
                .eq(user.getLevel() != null, User::getLevel, user.getLevel())
                .page(page);
        List<User> records = myPage.getRecords();
        List<UserVo> rows = new ArrayList<>();
        records.forEach(record->{
            UserVo userVo = new UserVo();
            userVo.setUsername(record.getUsername());
            userVo.setTelephone(record.getTelephone());
            userVo.setLevel(Integer.parseInt(record.getLevel()));
            userVo.setId(Long.valueOf(record.getId()));
            rows.add(userVo);
        });
        PageVo pageVo = new PageVo();
        pageVo.setTotal(myPage.getTotal());
        pageVo.setList(rows);
        return Result.succ(null,pageVo);
    }

    @Override
    public Result addUser(UserVo user) {
        User myUser = new User();
        User one = this.lambdaQuery().eq(User::getUsername, user.getUsername()).one();
        if (one!=null){
            return Result.fail("用户名已存在",null);
        }
        myUser.setLevel(String.valueOf(user.getLevel()));
        myUser.setUsername(user.getUsername());
        myUser.setTelephone(user.getTelephone());
        myUser.setPassword("123456");
        this.save(myUser);
        return Result.succ(null,null);
    }

    @Override
    public Result updateUser(UserVo user) {

        User myUser = new User();
        User one = this.lambdaQuery().eq(User::getId,user.getId()).one();
        if (one==null){
            return Result.fail("用户不存在",null);
        }
        myUser.setLevel(String.valueOf(user.getLevel()));
        myUser.setUsername(user.getUsername());
        myUser.setTelephone(user.getTelephone());
        myUser.setId(Math.toIntExact(user.getId()));
        this.updateById(myUser);
        return Result.succ(null,null);
    }

    @Override
    public Result deleteUserById(Long id) {
        User one = this.lambdaQuery().eq(User::getId, id).one();
        if (one==null){
            return Result.fail("用户不存在",null);
        }
        userMapper.deleteById(id);
        return Result.succ(null,null);
    }

    @Override
    public com.autumn.aitraffic.bean.User findUserByName(String username) {
        return null;
    }


}
